package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.utils.DateUtils;
import com.seegw.shaphar.INCService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SendGspJob extends AbstractJob {
    private static final Logger log = LoggerFactory.getLogger(SendGspJob.class);
    @Autowired
    ScheduledMapper scheduledMapper;
    @Autowired
    private INCService incService;
    @Autowired
    INcTaskLogService iNcTaskLogService;
    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    public void execute() {


        NcTaskLog taskLog = new NcTaskLog();
        String billno="";
        try {
            billno = "";
            taskLog.setTaskType("药品收货记录");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            incService.submitGspPurchasewe(1);
            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        } catch (Exception e) {
            log.error(e.getMessage());
            //throw new RuntimeException(e);
        }

        try {
            taskLog.setTaskType("药品拒收记录");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            incService.submitGspRgr(1);
            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        } catch (Exception e) {
            log.error(e.getMessage());
            //throw new RuntimeException(e);
        }

        try {
            taskLog.setTaskType("退货药品验收");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            incService.submitGspSaleReturnCheck(1);
            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        } catch (Exception e) {
            log.error(e.getMessage());
            //throw new RuntimeException(e);
        }

//        try {
//            taskLog.setTaskType("药品出库复核");
//            taskLog.setBillCount(0);
//            taskLog.setStartTime(DateUtils.now());
//            incService.submitGspOutCheck(1);
//            taskLog.setBillNo(billno);
//            taskLog.setEndTime(DateUtils.now());
//            iNcTaskLogService.save(taskLog);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//            //throw new RuntimeException(e);
//        }

        try {
            taskLog.setTaskType("药品验收记录");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            incService.submitGspPOCheck(1);
            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        } catch (Exception e) {
            log.error(e.getMessage());
            //throw new RuntimeException(e);
        }

        try {
            taskLog.setTaskType("养护记录");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            incService.submitKdlMaintainReport(1);
            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        } catch (Exception e) {
            log.error(e.getMessage());
            //throw new RuntimeException(e);
        }

        try {
            taskLog.setTaskType("不合格药品记录");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            incService.submitGspUnQualifiedRecord(1);
            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        } catch (Exception e) {
            log.error(e.getMessage());
            //throw new RuntimeException(e);
        }
    }
}
